package com.core.zyyz.amc.modules.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.util.TypeUtils;
import com.core.common.utils.StringUtils;
import com.core.common.utils.uuid.UUID;
import com.core.zyyz.util.CodeMakerUtil;
import com.core.zyyz.util.PublicUtil;
import org.apache.commons.collections4.MapUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.core.common.annotation.Log;
import com.core.common.core.controller.BaseController;
import com.core.common.core.domain.AjaxResult;
import com.core.common.enums.BusinessType;
import com.core.zyyz.amc.modules.domain.AmcCreditAgreement;
import com.core.zyyz.amc.modules.service.AmcCreditAgreementService;
import com.core.common.utils.poi.ExcelUtil;
import com.core.common.core.page.TableDataInfo;

/**
 * 授信额度Controller
 *
 * @author lfs
 * @date 2024-12-04
 */
@RestController
@RequestMapping("/amc/creditAgreement")
public class AmcCreditAgreementController extends BaseController
{
    @Autowired
    private AmcCreditAgreementService amcCreditAgreementService;

    /**
     * 查询授信额度列表
     */
    @PreAuthorize("@ss.hasPermi('amc:creditAgreement:list')")
    @GetMapping("/list")
    public TableDataInfo list(AmcCreditAgreement amcCreditAgreement) {
        startPage();
        List<AmcCreditAgreement> list = amcCreditAgreementService.selectAmcCreditAgreementList(amcCreditAgreement);
        return getDataTable(list);
    }

    /**
     * 获取授信额度详细信息
     */
    @PreAuthorize("@ss.hasPermi('amc:creditAgreement:query')")
    @GetMapping(value = "/{creditAgreementId}")
    public AjaxResult getInfo(@PathVariable("creditAgreementId") String creditAgreementId)
    {
        return success(amcCreditAgreementService.selectAmcCreditAgreementByCreditAgreementId(creditAgreementId));
    }

    /**
     * 新增授信额度
     */
    @PreAuthorize("@ss.hasPermi('amc:creditAgreement:add')")
    @Log(title = "授信额度", businessType = BusinessType.INSERT)
    @PostMapping("/addCreditAgreement")
    @Transactional
    public AjaxResult addCreditAgreement(@RequestBody Map<String, Object> paramMap) {
        PublicUtil pu = new PublicUtil();
        // pu.addPublicMap(paramMap, "add");
        AmcCreditAgreement amcCreditAgreement = TypeUtils.castToJavaBean(paramMap, AmcCreditAgreement.class);
        amcCreditAgreement.setCreditAgreementId(UUID.randomUUID().toString());
        //submitNode 页面传来的值用来区分授信方向 当页面传来的值为"1"时 表示银行贷款-授信方向为他方授信
        String submitNode = MapUtils.getString(paramMap, "submitNode");
        CodeMakerUtil cm = new CodeMakerUtil();
        amcCreditAgreement.setCreditAgreementCode(cm.bussCode("creditagreement"));
        if (StringUtils.isNotEmpty(submitNode) && submitNode.equals("1")) {
            amcCreditAgreement.setApprovalStatus("03");
            //银行贷款时，授信方向为 "他方授信"
            if ("01".equals(amcCreditAgreement.getCreditDirection())) {
                pu.updateProjectProcess(amcCreditAgreement.getRegisterNumber(), "creditAgreement1");
            }else {
                pu.updateProjectProcess(amcCreditAgreement.getRegisterNumber(), "creditAgreement2");
            }
        }
        //到期日
        String amountExpiryDate = MapUtils.getString(paramMap, "amountExpiryDate");
        //生效日
        String amountEffectiveDate = MapUtils.getString(paramMap, "amountEffectiveDate");
        if (StringUtils.isNotEmpty(amountEffectiveDate) && StringUtils.isNotEmpty(amountExpiryDate)) {
            long parse = Date.parse(amountExpiryDate);
            long parse1 = Date.parse(amountEffectiveDate);
            if (parse - parse1 < 0) {
                return error("额度生效日期不能晚于额度到期日期");
            }
        }
        return toAjax(amcCreditAgreementService.insertAmcCreditAgreement(amcCreditAgreement));
    }

    /**
     * 修改授信额度
     */
    @PreAuthorize("@ss.hasPermi('amc:creditAgreement:edit')")
    @Log(title = "授信额度", businessType = BusinessType.UPDATE)
    @PostMapping("/updateCreditAgreement")
    @Transactional
    public AjaxResult updateCreditAgreement(@RequestBody Map<String, Object> paramMap) {
        PublicUtil pu = new PublicUtil();
//        pu.addPublicMap(paramMap, null);
        AmcCreditAgreement amcCreditAgreement = TypeUtils.castToJavaBean(paramMap, AmcCreditAgreement.class);
        String submitNode = MapUtils.getString(paramMap, "submitNode");
        if (StringUtils.isNotEmpty(submitNode) && submitNode.equals("1")) {
                amcCreditAgreement.setApprovalStatus("03");
                //将节点更新至项目库
                if ("01".equals(amcCreditAgreement.getCreditDirection())) {
                    pu.updateProjectProcess(amcCreditAgreement.getRegisterNumber(), "creditAgreement1");
                } else {
                    pu.updateProjectProcess(amcCreditAgreement.getRegisterNumber(), "creditAgreement2");
                }
        }
        //到期日
        String amountExpiryDate = MapUtils.getString(paramMap, "amountExpiryDate");
        //生效日
        String amountEffectiveDate = MapUtils.getString(paramMap, "amountEffectiveDate");
        if (StringUtils.isNotEmpty(amountEffectiveDate) && StringUtils.isNotEmpty(amountExpiryDate)) {
            long parse = Date.parse(amountExpiryDate);
            long parse1 = Date.parse(amountEffectiveDate);
            if (parse - parse1 < 0) {
                return error("额度生效日期不能晚于额度到期日期");
            }
        }
        return toAjax(amcCreditAgreementService.updateAmcCreditAgreement(amcCreditAgreement));
    }

    /**
     * 删除授信额度
     */
    @PreAuthorize("@ss.hasPermi('amc:creditAgreement:remove')")
    @Log(title = "授信额度", businessType = BusinessType.DELETE)
    @DeleteMapping("/{creditAgreementIds}")
    public AjaxResult remove(@PathVariable String[] creditAgreementIds)
    {
        return toAjax(amcCreditAgreementService.deleteAmcCreditAgreementByCreditAgreementIds(creditAgreementIds));
    }
}
